﻿using EmoneyWeiXinCommon;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace EmoneyWeiXinDAL
{
    public class QQKeyDAL
    {
        public static int Insert(string qqNames, string key)
        {
            string sql = "INSERT INTO QQKeys values(@QQName,@QQKey,0,GETDATE())";
            SqlParameter[] paras = 
            { 
              new SqlParameter("@QQName",qqNames),
              new SqlParameter("@QQKey",key)
            };
            return SQLHelper.ExecuteNonQuery(Config.EmoneyWeiXin, System.Data.CommandType.Text, sql, paras);
        }

        public static int Delete(string key)
        {
            string sql = "DELETE QQKeys WHERE QQKey=@QQKey";
            SqlParameter[] paras = { 
                                   new SqlParameter("@QQKey",key)
                                   };
            return SQLHelper.ExecuteNonQuery(Config.EmoneyWeiXin, System.Data.CommandType.Text, sql, paras);
        }

        public static int SetStatusTo1(string QQKey)
        {
            string sql = "UPDATE QQKeys SET Status=1 WHERE QQKey=@QQKey;UPDATE QQKeys SET Status=0 WHERE QQKey<>@QQKey";
            SqlParameter[] paras = 
            {
              new SqlParameter("@QQKey",QQKey)
            };
            return SQLHelper.ExecuteNonQuery(Config.EmoneyWeiXin, System.Data.CommandType.Text, sql, paras);
        }

        public static DataTable SelectForPage(int pageSize, int pageIndex, out int totalCount)
        {
            string sql = "proc_Pagination";
            SqlParameter dataCountPara = new SqlParameter();
            dataCountPara.Size = 10;
            dataCountPara.Direction = ParameterDirection.Output;
            dataCountPara.ParameterName = "@DataCount";
            SqlParameter[] paras = 
            {
              new SqlParameter("@pageSize",pageSize),
              new SqlParameter("@keyName","QQKey"),
              new SqlParameter("@pageIndex",pageIndex),
              new SqlParameter("tableName","QQKeys"),
              new SqlParameter("@orderby","CreateTime"),
              new SqlParameter("@whereText",""),
              new SqlParameter("@Filter",""),
              dataCountPara
            };
            DataTable dt = SQLHelper.ExecuteGetTable(Config.EmoneyWeiXin, System.Data.CommandType.StoredProcedure, sql, paras);
            totalCount = Convert.ToInt32(dataCountPara.Value);
            return dt;
        }

        public static string GetQiyongKey()
        {
            string sql = "select top 1 QQKey FROM QQKeys with(nolock) where Status=1";
            return SQLHelper.ExecuteScalar(Config.EmoneyWeiXin, System.Data.CommandType.Text, sql, null).ToString();
        }
    }
}
